<template>
  <div>
    <!-------------------------------表格 ----------------------down ------------------->
    <el-card :body-style="{padding:'0px 8px'}" style="background-color: #f3f3f3">
      <!-------------------------------按钮 ----------------------down------------------->
      <div>
        <el-button type="info" @click="add">新建</el-button>
        <el-input v-model="query.name" class="handle-input" placeholder="模板名称" />
        <el-button class="hand-btn" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </div>
      <!-------------------------------按钮 ----------------------up ------------------->
      <el-table
        ref="multipleTable"
        :data="page.content"
        border
        max-height="580"
        height="580"
        :cell-style="{'padding':'2px','font-size':'12px'}"
        :header-cell-style="{'padding':'0px','font-weight':'800'}"
        class="table"
        header-cell-class-name="table-header"
      >
        <el-table-column prop="id" label="ID" sortable width="80" />
        <el-table-column prop="name" label="模板名称" width="200" />
        <el-table-column prop="version" label="版本" width="220" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column label="操作" width="120" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-------------------------------表格 ----------------------up ------------------->
      <el-card :body-style="{padding:'5px 20px'}">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next"
          :page-sizes="[10,20, 30, 40, 50]"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="page.totalElements"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        />
      </el-card>
    </el-card>
  </div>
</template>
<script>
import { query } from '@/api/report'
import { delbyid } from '@/api/report'
export default {
  name: 'FormworkIndex',
  data() {
    return {
      open: false,
      open1: false,
      selPlans: [],
      query: {
        pageIndex: 1,
        pageSize: 20,
        name: ''
      },
      page: {}
    }
  },
  created() {
    console.log(this.$route.name)
    if (this.$route.name === 'ipqcjyReportIndex') {
      this.query.mold = 'IPQC检验'
    } else if (this.$route.name === 'ipqcxjReportIndex') {
      this.query.mold = 'IPQC巡验'
    } else if (this.$route.name === 'produceReportIndex') {
      this.query.mold = '产品检验'
    } else if (this.$route.name === 'oldproduceReportIndex') {
      this.query.mold = '产品老化'
    } else if (this.$route.name === 'produceoutReportIndex') {
      this.query.mold = '产品出厂'
    } else if (this.$route.name === 'customerReportIndex') {
      this.query.mold = '顾客投诉'
    } else if (this.$route.name === 'pphandleReportIndex') {
      this.query.mold = 'PP每日维修'
    } else if (this.$route.name === 'ppfirstReportIndex') {
      this.query.mold = 'PP组装首检'
    }
    this.getData()
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
      query(this.query).then(res => {
        this.page = res.data.page
      })
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, 'pageIndex', 1)
      this.getData()
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      }).then(() => {
        delbyid(row.id).then(res => {
          this.$message.success('删除成功')
          this.getData()
        })
      })
        .catch(() => {
        })
    },
    // 编辑操作
    handleEdit(index, row) {
      if (this.$route.name === 'ipqcjyReportIndex') {
        this.$router.push({ path: 'ipqcjyReportEdit', query: { id: row.id }})
      } else if (this.$route.name === 'ipqcxjReportIndex') {
        this.$router.push({ path: 'ipqcxjReportEdit', query: { id: row.id }})
      } else if (this.$route.name === 'produceReportIndex') {
        this.$router.push({ path: 'produceReportEdit', query: { id: row.id }})
      } else if (this.$route.name === 'oldproduceReportIndex') {
        this.$router.push({ path: 'oldproduceReportEdit', query: { id: row.id }})
      } else if (this.$route.name === 'produceoutReportIndex') {
        this.$router.push({ path: 'produceoutReportEdit', query: { id: row.id }})
      } else if (this.$route.name === 'customerReportIndex') {
        this.$router.push({ path: 'customerReportEdit', query: { id: row.id }})
      } else if (this.$route.name === 'pphandleReportIndex') {
        this.$router.push({ path: 'pphandleReportEdit', query: { id: row.id }})
      } else if (this.$route.name === 'ppfirstReportIndex') {
        this.$router.push({ path: 'ppfirstReportEdit', query: { id: row.id }})
      }
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, 'pageIndex', val)
      this.getData()
    },
    // 分页大小改变
    handleSizeChange(val) {
      this.$set(this.query, 'pageSize', val)
      this.getData()
    },
    add() {
      if (this.$route.name === 'ipqcjyReportIndex') {
        this.$router.push({ path: 'ipqcjyReportEdit' })
      } else if (this.$route.name === 'ipqcxjReportIndex') {
        this.$router.push({ path: 'ipqcxjReportEdit' })
      } else if (this.$route.name === 'produceReportIndex') {
        this.$router.push({ path: 'produceReportEdit' })
      } else if (this.$route.name === 'oldproduceReportIndex') {
        this.$router.push({ path: 'oldproduceReportEdit' })
      } else if (this.$route.name === 'produceoutReportIndex') {
        this.$router.push({ path: 'produceoutReportEdit' })
      } else if (this.$route.name === 'customerReportIndex') {
        this.$router.push({ path: 'customerReportEdit' })
      } else if (this.$route.name === 'pphandleReportIndex') {
        this.$router.push({ path: 'pphandleReportEdit' })
      } else if (this.$route.name === 'ppfirstReportIndex') {
        this.$router.push({ path: 'ppfirstReportEdit' })
      }
    }
  }
}
</script>

<style scoped>
  .handle-box {
    margin: 20px;
  }

  .handle-select {
    width: 120px;
  }

  .handle-input {
    margin: 10px;
    width: 300px;
    display: inline-block;
  }
  .table {
    width: 100%;
    font-size: 14px;
  }
  .red {
    color: #ff0000;
  }
  .hand-btn {
    margin: 5px;
  }
</style>
